package irri.soils.ssnm;

import irri.soils.ssnm.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class ActivityI extends SSNM implements View.OnClickListener{
	
	private RadioButton radio1;
	private RadioButton radio2;
	private RadioButton radio3;
	private int option;
	private Button button1;
	private Bundle b;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.i);
        setCustomTitle();
        b = this.getIntent().getExtras();
        int ce = b.getInt("CE");
        ImageView image1 = (ImageView) findViewById(R.id.imageView1);
        ImageView image2 = (ImageView) findViewById(R.id.imageView2);
        if(ce==1) image1.setVisibility(0); else image2.setVisibility(0);
        TextView tv = (TextView) findViewById(R.id.question);
        tv.setText(get("GW0")+" "+(ce==1 ? get("GW1") : get("GW2"))+" "+get("GW3"));
        int se = ce==1 ? b.getInt("SE") : 0;
        int selected = ce*10+se;
        String options[] = {};
        switch(selected){
        case 11: options = getResources().getStringArray(getArrayCode("tp_less23")); break;
        case 12: options = getResources().getStringArray(getArrayCode("tp_greater23")); break;
        case 20: options = getResources().getStringArray(getArrayCode("ds_rc1")); break;
        }
        
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio1.setOnClickListener(this);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radio2.setOnClickListener(this);
        radio3 = (RadioButton) findViewById(R.id.radio3);
        radio3.setOnClickListener(this);
        radio1.setText(options[0]);
        radio2.setText(options[1]);
        radio3.setText(options[2]);
        
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button1.setEnabled(false);
        
        b = this.getIntent().getExtras();
        if(b.containsKey("GW")){
	        switch(b.getInt("GW")){
	        case 1: radio1.performClick(); break;
	        case 2: radio2.performClick(); break;
	        case 3: radio3.performClick(); break;
	        }
        }
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.radio1: 
			option = 1; 
			button1.setEnabled(true); 
			break;
		case R.id.radio2:
			option = 2; 
			button1.setEnabled(true);
			break;
		case R.id.radio3:
			option = 3; 
			button1.setEnabled(true);
			break;
		case R.id.button1:
			Intent intent = new Intent(
					ActivityI.this,
					ActivityJ.class
				);
			b.putInt("GW", option);
			intent.putExtras(b);
			startActivity(intent);
			finish();
			break;
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(radio1.isChecked()) radio1.performClick();
		if(radio2.isChecked()) radio2.performClick();
		if(radio3.isChecked()) radio3.performClick();
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(
				ActivityI.this,
				b.containsKey("SE") ? ActivityH.class : ActivityG.class
			);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}
}